<?php
namespace app\agent\model;
use think\Request;
use app\lib\exception\LoginException;
use app\lib\exception\SuccessMessage;


class Key extends BaseModel{

    //获取首页内容
    public function getIndexContent() {
        $agent = Agent::where('status', 'neq', '0')->where(array('uper_id' => session('agentId')))->select();
        for($i=0; $i<count($agent); $i++) {
            $agent[$i]['total'] = Key::where('is_active', 'neq', '-1')->where(array('agent_id' => $agent[$i]['id']))->count();
            $agent[$i]['enabled'] = Key::where('is_active', 'eq', '1')->where(array('agent_id' => $agent[$i]['id']))->count();
        }

        return $agent;
    }

    //分配激活码
    public function distributeCode($id, $name, $total) {
        $code = Key::where(array('agent_id' => session('agentId'), 'is_active' => 0))->limit($total)->select();
        //dump($code);exit;
        $res = 0;
        $codeId = array();
        for($i=0; $i<count($code); $i++) {
            $res += Key::where(array('id' => $code[$i]['id']))->update(array('agent_id' => $id, 'agent_name' => $name));
            $codeId[$i] = $code[$i]['id'];
        }
        $returnData['num'] = $res;
        $returnData['data'] = implode(',', $codeId);
        return $returnData;

    }
}